package com.alibaba.datax.core.statistics;

import java.util.concurrent.TimeUnit;

import com.codahale.metrics.ConsoleReporter;
import com.codahale.metrics.MetricRegistry;
import com.codahale.metrics.Reporter;

import cn.jwis.tp.obs.metrics.exporter.DefaultSchedulerReporter;
import cn.jwis.tp.obs.metrics.exporter.MetricHolder;
import cn.jwis.tp.obs.metrics.exporter.NopSchedulerReporter;

/**
 * @类名: MetricsReporterConfiguration
 * @说明: metrics构建
 *
 * @author   leehom
 * @Date	 2021年11月25日 上午11:35:25
 * 修改记录：
 *
 * @see 	 
 */
public class MetricsConfiguration {
	
    // MetricRegistry
	public static final MetricRegistry metricsReg = new MetricRegistry();
	private static MetricHolder metricHolder;
	public static Reporter report;
	
	// 报告器
	// nop reporter
    public static Reporter nopReporter() {
    	NopSchedulerReporter reporter = NopSchedulerReporter.forRegistry(metricsReg).build();
    	return reporter;
    }
    
	// 控制台reporter
    public static Reporter consoleReporter(int reprotInterval) {
    	ConsoleReporter reporter = ConsoleReporter.forRegistry(metricsReg).build();
    	reporter.start(reprotInterval, TimeUnit.SECONDS);
    	return reporter;
    }
    
	// 定时报告器
    public static Reporter schedulerReporter(String url, String job, int reprotInterval) {
        DefaultSchedulerReporter reporter = DefaultSchedulerReporter.forRegistry(metricsReg)
        		.url(url)
        		.job(job)
        		.tag(new DataxTagExtractor())
        		.build();
        reporter.start(reprotInterval, TimeUnit.SECONDS);
    	return reporter;
    }
	
    // 指标存放
    public static MetricHolder metricHolder() {
    	if(metricHolder!=null)
    		return metricHolder;
    	MetricHolder h = new MetricHolder();
    	h.setRegistry(metricsReg);
    	h.init();
    	metricHolder = h;
    	return h;
    } 
    
    public static String metricKey(String jobId) {
    	return "datax_setl"+jobId;
    }
    
    public static String metricKey(Long jobId) {
    	return "datax_setl"+jobId;
    }
    
}
